package org.ling.xuan.action;


import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import org.jetbrains.annotations.NotNull;
import org.ling.xuan.condition.LxCondition;
import org.ling.xuan.dao.MarkFileDao;
import org.ling.xuan.util.LoggerUtil;
import org.ling.xuan.util.LxOptional;


/**
 * 标记为关联文件
 *
 * @author liting50684
 * @date 2023/08/21
 */
public class LxLinkAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        String basePath = LxOptional.of(e, c -> c.getProject().getBasePath()).orElse("");
        String filePath = LxOptional.of(e, c -> c.getDataContext().getData(CommonDataKeys.VIRTUAL_FILE).getPath()).orElse("");
        if (StrUtil.isNotBlank(basePath) && StrUtil.isNotBlank(filePath)) {
            MarkFileDao.mark(basePath, filePath, null, true,"");
            LoggerUtil.log(e.getProject(), "标记为关联文件成功:{}", filePath);
        } else {
            LoggerUtil.log(e.getProject(), "无效的mark操作..");
        }

    }

    @Override
    public void update(AnActionEvent e) {
        boolean enable = LxCondition.getInstance().markCondition(e);
        e.getPresentation().setEnabledAndVisible(enable);
    }
}
